package com.cf.spider.core.model.dao;

import com.cf.crud.mapper.BaseMapper;
import com.cf.spider.core.model.JobTask;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface JobTaskDao extends BaseMapper<JobTask> {
    @Select("select * from jobTask where state in ('prepare','error') and fetchRetry <= #{maxFetchNum} ")
    List<JobTask> getAllFetchTask(@Param("maxFetchNum") int maxFetchNum);


    @Update("<script>" +
            "update jobTask set state='doing', fetchRetry=fetchRetry+1 where id in " +
            "<foreach collection='jobTasks' item='item' index='index' open='(' separator=',' close=')'>" +
            "#{item.id}" +
            "</foreach>" +
            "</script>"
    )
    void prepare(@Param("jobTasks") List<JobTask> jobTasks);

    @Select("select * from jobTask where state='success' and pushRetry <= #{maxPushNum} ")
    List<JobTask> getAllPushTask(@Param("maxPushNum") int maxPushNum);


    @Update("<script>" +
            "update jobTask set pushRetry=pushRetry+1 where id in " +
            "<foreach collection='jobTasks' item='item' index='index' open='(' separator=',' close=')'>" +
            "#{item.id}" +
            "</foreach>" +
            "</script>"
    )
    void retryPush(@Param("jobTasks")List<JobTask> allFetchTask);
}
